/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.raytracer;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author drafkind
 */
public class Plane extends VisibleObject {
  private Vector3 normal;
  private double distance;

  public Plane() {
  }

  public Plane(Vector3 n, double d) {
    normal = n;
    distance = d;
  }

  public List<IntersectionPoint> getIntersectionPoints(Ray ray) {
    List<IntersectionPoint> points = new ArrayList<IntersectionPoint>();

    double denom = ray.getDirection().dot(normal);

    if (denom != 0.0) {
      double numer = -(ray.getPosition().dot(normal) + distance);
      double t = numer / denom;
      if (t > 0) {
        Vector3 point = ray.getPosition().plus(ray.getDirection().times(t));
        points.add(
                new IntersectionPoint(
                  point,
                  point.getDistanceTo(ray.getPosition()),
                  this));
      }
    }

    return points;
  }

  @Override public boolean isInsideImplementation(Vector3 where) {
    return (normal.dot(where) + distance) <= Util.EPSILON;
  }

  public double getDistance() {
    return distance;
  }

  public void setDistance(double distance) {
    this.distance = distance;
  }

  public Vector3 getNormal() {
    return normal;
  }

  public void setNormal(Vector3 normal) {
    this.normal = normal.getNormalized();
  }

  public Vector3 getNormalAt(IntersectionPoint where) {
    return normal;
  }
}